<?php
/**
 * Get videos with keywords
 * Send an Array of Mikubook_Video in JSON
 *
 * If an error occurend, send an object with:
 * - error_type: (string) type of error, mainly 'connection'
 * - error: (string) the error
 */
require_once("../include/setup.php");
require_once(CONTROLLER_PATH.'mikubook.controller.php');

Mikubook_Video_Controller::$LOGIN = $_REQUEST['login'];
Mikubook_Video_Controller::$PASSWORD = $_REQUEST['password'];

$c = new Mikubook_Video_Controller();

$videos = $c->getVideos(array(
	'keywords' => $_REQUEST['keywords'], 
	'offset' => $_REQUEST['offset'], 
	'sort' => $_REQUEST['sort'], 
	'cached' => true
));
foreach ($videos as $v) {
	$v->linkEmbed = $v->getLinkEmbed(array('autoplay' => 1, 'showinfo' => 0));
}
$c->close();


if (!$c->connected) {
	$videos = array(
		'error_type' => 'connection',
		'error' => $c->error_connection
	);
}

echo json_encode($videos);
?>